define(function(require, exports, module) {
	'use strict';
	
	var rk = require('rk');
	
	//require('../announcement/announcementPanelCtrl');
	var uService = require('core/services/biz/noticeService');
	var getTimeToString = uService.getTimeToString;
	$.widget('rk.announcementContentCtrl', $.rk.oaContentCtrl, {
		_language: "zh",
		_create: function () {
			var me = this;
			var elem = me.element;
			if (navigator.userLanguage) {
				me._language = navigator.userLanguage.substring(0, 2).toLowerCase();
			} else {
				me._language = navigator.language.substring(0, 2).toLowerCase();
			}
		},
		_init: function () {
			var me = this;
			var elem = me.element;
			var opt = me.options;
			if (opt.type == "announcement") {
				var elemParent = elem.parent();
				if(elemParent && elemParent.hasClass("oa-content")){
					elemParent.removeClass("oa-content-800");
					elemParent.addClass("oa-announcement-800");
				}
				rk.post("/json/oa_announcement/detail.action", {aid: opt.id}, function (json) {
					if (json.status === 0) {
						var created = json.body.created;
						var nowTime = json.nowTime;
						json.body.created = me.getTimeToString(created, nowTime, me);
						var createTime = new Date(created);
						json.body.createTime = createTime.getFullYear() + "年" + (createTime.getMonth() + 1) + "月" + createTime.getDate() + "日";
						if (json.body.stickDate) {
							var stickDate = json.body.stickDate;
							var stickDateTime = new Date(stickDate);
							var month = (stickDateTime.getMonth() + 1) + "";
							month = month.length == 1 ? "0" + month : month;
							var day = stickDateTime.getDate() + "";
							day = day.length == 1 ? "0" + day : day;
							json.body.stickDate = stickDateTime.getFullYear() + "-" + month + "-" + day;
						}
						//var html = rk.template("tpl_noti_announcement_detail", json);
						var temp = require('oa/tmpl/announcement/template/tpl_announcement_detail.tpl');
						var html = rk.templateText(temp,json);
						elem.html(html);
						elem.find(".oa-announce-body").beScroll({
							axis:'yx'
						});
					} else {
						rk.noticeError(rk.i18n('ERROR_SYSTEM'));
					}
				});
				me._initAnnouncementListner();
			}
		},
		_initAnnouncementListner: function () {
			var me = this;
			var elem = me.element;
			var opt = me.options;
			elem.on('click', "[act='set_top']", function () {
				elem.find("[act='set_top_div']").toggle();
				var daysHtml = elem.find("[act='ann_update_stickType_days']").html();
				if (daysHtml === "") {
					var stick_days = {
						3: rk.i18n('ANNOUNCEMENT_TIME_THREE_DAY'),
		                7: rk.i18n('ANNOUNCEMENT_TIME_ONE_WEEK'),
		                14: rk.i18n('ANNOUNCEMENT_TIME_TWO_WEEK'),
		                30: rk.i18n('ANNOUNCEMENT_TIME_ONE_MONTH'),
		                60: rk.i18n('ANNOUNCEMENT_TIME_TWO_MONTH'),
		                90: rk.i18n('ANNOUNCEMENT_TIME_THREE_MONTH'),
		                180: rk.i18n('ANNOUNCEMENT_TIME_SIX_MONTH'),
		                360: rk.i18n('ANNOUNCEMENT_TIME_ONE_YEAR'),
		                "-1": rk.i18n('ANNOUNCEMENT_TIME_FOREVER')
					};
					var stick_daysHtml = "";
					var daysValue = elem.find("[act='ann_update_stickType_days_hidden']").val();
					for (var key in stick_days) {
						if (key === daysValue) {
							stick_daysHtml += "<option value='" + key + "' selected>" + stick_days[key] + "</option>"
						} else {
							stick_daysHtml += "<option value='" + key + "'>" + stick_days[key] + "</option>"
						}
					}
					elem.find("[act='ann_update_stickType_days']").html(stick_daysHtml);
					if (daysValue) {
						elem.find("[act='ann_update_stickType_days']").val(daysValue);
					}
					elem.find("[act='ann_update_stickType_days']").selectmenu();
					elem.find(".ui-check-buttonset").buttonset();
					elem.find(".ui-radio-buttonset").buttonset();
				}
				if (!elem.find("[act='ann_update_stickType_date']").val()) {
					var nowDate = new Date();
					var month = (nowDate.getMonth() + 1) + "";
					month = month.length == 1 ? "0" + month : month;
					var day = nowDate.getDate() + "";
					day = day.length == 1 ? "0" + day : day;
					var defaultDate = nowDate.getFullYear() + "-" + month + "-" + day;
					elem.find("[act='ann_update_stickType_date']").val(defaultDate);
				}
				elem.find("[act='ann_update_stickType_date']").datepicker();
			});
			elem.on('click', "[act='anno_delete']", function () {
				var del = $(this);
				rk.confirm(rk.i18n('NOTICE_CONFIRM_DELETE'),{okText:rk.i18n('DELETE')}, function () {
					rk.post("/json/oa_announcement/delete.action", {aid: del.attr("aid")}, function (json) {
						if (json.status === 0) {
							//删除成功，刷新
							var panel = rk.oa.getPanel();
							if(opt.come == "notice") {
								if (panel) {
									require.async(['../notice/notiPanelCtrl.js'],function(){
										panel.notiPanelCtrl("loaddata", "announcement");
									});
								}
							}else{
								if (panel) {
									require.async(["../announcement/announcementPanelCtrl"], function () {
										panel.announcementPanelCtrl("loaddata", "announcement");
									});
								}
							}
							rk.noticeSuccess(rk.i18n('OPERATE_SUCCESS'));
							me.closeMe();
						} else if (json.status === 211001) {
							//没有权限
							rk.noticeError(rk.i18n('NOTICE_NO_ACCESS'));
						} else if (json.status === 100000) {
							//系统错误
							rk.noticeError(rk.i18n('ERROR_SYSTEM'));
						}
					});
				}, function () {
					//取消
				});
			});
			elem.on('click', '[act="ann_update_stickType_days_radio"]', function () {
				elem.find("[act='ann_update_stickType_date']").attr("disabled", "disabled");
				elem.find("[act=ann_update_stickType_days]").attr("disabled", null);
				elem.find("[act='ann_update_stickType_days']").selectmenu("enable");
			});
			elem.on('click', '[act="ann_update_stickType_date_radio"]', function () {
				elem.find("[act='ann_update_stickType_days']").attr("disabled", "disabled");
				elem.find("[act='ann_update_stickType_date']").attr("disabled", null);
				elem.find("[act='ann_update_stickType_days']").selectmenu("disable");
			});
			elem.on("click", "[act='ann_update_stick_check']", function(){
				var dateRadio = elem.find("[act='ann_update_stickType_date_radio']");
				var daysRadio = elem.find("[act='ann_update_stickType_days_radio']");
				if(dateRadio.attr("disabled") && daysRadio.attr("disabled")) {
					dateRadio.attr("disabled", null);
					daysRadio.attr("disabled", null);
					dateRadio.button({disabled: false});
					daysRadio.button({disabled: false});
					if(dateRadio.is(":checked")){
						elem.find("[act='ann_update_stickType_date']").attr("disabled", null);
					}
					if(daysRadio.is(":checked")){
						elem.find("[act='ann_update_stickType_days']").selectmenu("enable");
					}
				}else{
					dateRadio.attr("disabled", "disabled");
					daysRadio.attr("disabled", "disabled");
					dateRadio.button({disabled: true});
					daysRadio.button({disabled: true});
					elem.find("[act='ann_update_stickType_days']").selectmenu("disable");
					elem.find("[act='ann_update_stickType_date']").attr("disabled", "disabled");
				}
			});
			elem.on('click', "[act='ann_update_submit']", function () {
				var form = elem.find("[act='ann_update_form']");
				var formData = form.serializeArray();
				var params = {};
				$.each(formData, function (i, json) {
					if (json.name === "stickDate" && json.value) {
						var value = json.value;
						var valueArray = value.split("-");
						var year = valueArray[0];
						var month = valueArray[1];
						var day = valueArray[2];
						json.value = new Date(year - 0, month - 1, day - 0).getTime();
					}
					params["announcement." + json.name] = json.value;
				});
				rk.post("/json/oa_announcement/update-announcement.action", params, function (json) {
					if (json.status === 0) {
						//置顶成功
						var panel = rk.oa.getPanel();
						if(opt.come == "notice") {
							if (panel) {
								require.async(['../notice/notiPanelCtrl.js'],function(){
									panel.notiPanelCtrl("loaddata", "announcement");
								});
							}
						}else{
							if (panel) {
								require.async(["../announcement/announcementPanelCtrl"],function(){
									panel.announcementPanelCtrl("loaddata", "announcement");
								});
							}
						}
						elem.find("[act='set_top_div']").hide();
						rk.noticeSuccess(rk.i18n('ANNOUNCEMENT_TOP_SUCCESS'));
					} else if (json.status === 211001) {
						//没有权限
						rk.noticeError(rk.i18n('NOTICE_NO_ACCESS'));
					} else if (json.status === 100000) {
						//系统错误
						rk.noticeError(rk.i18n('ERROR_SYSTEM'));
					}
				});
			});
		},
		getTimeToString: getTimeToString
	});
});